<template>
    <div :class="{ PC: isPc, mobile: !isPc, 'nav-box': true }" :style="{ backgroundImage: `url(${bgURL})` }">
        <jnav @changeCategory="changeCategory" />
        <div class="search-box">
            <input type="text" v-model="searchWord" @keyup.enter="handleSearch" />
        </div>
        <template v-if="!isMobile()">
            <div class="wave" id="wave1"></div>
            <div class="wave" id="wave2"></div>
        </template>
    </div>
</template>

<script setup>
import { get } from "@/util/request.js";
import { onMounted, ref } from "vue";
import isMobile from "../util/isMobile.js";
import jnav from "./nav.vue";

const categories = ref([]);
const emit = defineEmits(["handle", "search"]);
const bgURL = ref("");
const searchWord = ref(null);
const isPc = ref(true);

onMounted(() => {
    get("/category/get").then((resp) => {
        categories.value = resp.insts;
    });
    if (isMobile()) {
        isPc.value = false;
    } else {
        bgURL.value = "https://api.paugram.com/wallpaper/";
    }
});

const changeCategory = (id) => {
    emit("handle", id);
};

const handleSearch = () => {
    get("/article/search?txt=" + searchWord.value).then((resp) => {
        emit("search", resp.insts);
    });
};
</script>

<style scoped>
.nav-box {
    display: flex;
    justify-content: end;
    align-items: start;
    align-content: start;
    flex-wrap: wrap;
    height: 100vh;
    background-attachment: fixed;
    background-position: center center;
    background-size: cover;
    position: relative;
}
.mobile.nav-box {
    align-items: center;
}

.search-box {
    position: absolute;
    left: 0;
    text-align: center;
    top: 50%;
    width: 100%;
}

.search-box > input {
    width: 20%;
    cursor: pointer;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 10px;
    border: 0;
    padding: 20px 30px;
    color: #fff;
    font-size: 16px;
}

@media screen and (max-width: 800px) {
    .nav-box {
        height: fit-content;
        background-size: 100% 100%;
    }

    .search-box {
        position: relative;
        flex: 1;
    }

    .search-box > input {
        width: 80%;
    }
}

.search-box > input:focus {
    background: rgba(0, 0, 0, 0.5);
}

.wave {
    position: absolute;
    bottom: 0;
    animation: waveA 120s linear infinite;
}

#wave1 {
    height: 84px;
    width: 200%;
    z-index: 10;
    background-image: url("../static/wave1.png");
}

#wave2 {
    height: 100px;
    width: 400%;
    z-index: 5;
    background-image: url("../static/wave2.png");
}

@keyframes waveA {
    0% {
        background-position: 0 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0 50%;
    }
}
</style>
